// import axios, { AxiosInstance, AxiosPromise, AxiosRequestConfig, AxiosResponse } from 'axios'
import { request } from './request'
import { message } from 'antd'
import config from '../../config'

const MODE = import.meta.env.MODE

export async function invokeApi<T>(path: string, ...params: any[]): Promise<ApiResponse<T>> {
    const { token, tokenHead } = $store.getState().auth
    //@ts-ignore
    const base = config[MODE]

    return new Promise<ApiResponse<T>>((resolve, reject) => {
        request(base.apiBaseUrl + path, {
            method: 'POST',
            withCredentials: true,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/json',
                Authorization: `${token}${tokenHead}`,
            },
            data: { args: params }
        }).then(res => {
            if (!res.data.success) {
                return reject(res)
            }
            return resolve(res.data)

        }).catch(e => {
            message.error('前方拥挤，请刷新再试')
            return reject(e)
        })
    })
}